package com.javaweb.oa02.user;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/dept/exit")
public class exit extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        HttpSession session=req.getSession(false);
        if(session!=null) {
            //从session域中移除userSession对象
            session.removeAttribute("user1");
            //手动销毁session对象
            session.invalidate();
        }
        //删除cookie
        Cookie[] cookies= req.getCookies();
        if(cookies!=null){
            for(Cookie cookie:cookies){
                cookie.setMaxAge(0);
                //删除cookie时，要注意路径问题
                cookie.setPath(req.getContextPath());
                resp.addCookie(cookie);
            }
        }
        resp.sendRedirect(req.getContextPath());
        //删除cookie
      /*  Cookie cookie1=new Cookie("username","");
        Cookie cookie2=new Cookie("password","");

        cookie1.setMaxAge(0);
        cookie2.setMaxAge(0);

        cookie1.setPath(req.getContextPath());
        resp.sendRedirect(req.getContextPath() );

        resp.addCookie(cookie2);
        resp.addCookie(cookie1);*/

    }
}
